package com.mdc.mansion.api.web.modules.restaurant;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mdc.mansion.api.web.common.web.BaseController;
import com.mdc.mansion.common.exception.BusinessException;
import com.mdc.mansion.common.response.Result;
import com.mdc.mansion.common.response.ResultCode;
import com.mdc.mansion.common.util.I18nUtil;
import com.mdc.mansion.core.modules.basedata.TableQrCodeService;
import com.mdc.mansion.core.modules.restaurant.RestaurantTableService;
import com.mdc.mansion.core.modules.restaurant.dto.RestaurantTableDTO;
import com.mdc.mansion.dataaccess.common.context.ContextHolder;
import com.mdc.mansion.dataaccess.modules.restaurant.entity.Restaurant;
import com.mdc.mansion.dataaccess.modules.restaurant.entity.RestaurantTable;
import com.mdc.mansion.dataaccess.modules.restaurant.entity.mo.RestaurantTableMO;

/**
 *
 * @author xizf
 *
 */
@Controller
@RequestMapping(value = "${adminPath}/table")
public class RestaurantTableController extends BaseController {
    @Autowired
    private RestaurantTableService restaurantTableService;
    @Resource
    private TableQrCodeService tableQrCodeService;
    @RequestMapping("/status")
    public Result status(String data) {
        Result result = new Result(ResultCode.COMMON_SUCCESS);
        String restaurantId = ContextHolder.currentRestaurantId();
//        String restaurantId = "98fd5657ac264aada5aeb00c2f2cf487";
        JSONArray jsonArray = new JSONArray();
        String restaurantTableId =null;
        JSONObject jObject = JSONObject.parseObject(data);
        if(jObject.containsKey("restaurantTableId")) {restaurantTableId =jObject.getString("restaurantTableId");}
        if (restaurantId != null && !"".equals(restaurantId)) {
            List<RestaurantTableMO> tableStatusList = new ArrayList<>();
            tableStatusList = restaurantTableService.selectRestaurantTableWithStatus(restaurantId,restaurantTableId);
            if (tableStatusList != null && tableStatusList.size() > 0) {
                for (RestaurantTableMO restaurantTableMO : tableStatusList) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("id", restaurantTableMO.getId());
                    jsonObject.put("status", restaurantTableMO.getStatus());
                    jsonObject.put("num", restaurantTableMO.getNum());
                    jsonObject.put("name", restaurantTableMO.getName());
                    jsonObject.put("englishName", restaurantTableMO.getEnglishName());
                    jsonObject.put("chineseName", restaurantTableMO.getChineseName());
                    jsonArray.add(jsonObject);
                }
            }
        }
        result.setProperty("tableList", jsonArray);
        return result;
    }

    @RequestMapping("/list")
    public Result list(HttpServletRequest request) {

        Result result = new Result(ResultCode.COMMON_SUCCESS);

        String restaurantId = ContextHolder.currentRestaurantId();

        JSONArray jsonArray = new JSONArray();

        if (restaurantId != null && !"".equals(restaurantId)) {

            List<RestaurantTableMO> tableStatusList = new ArrayList<>();

            tableStatusList = restaurantTableService.selectRestaurantTableWithStatus(restaurantId,null);

            if (tableStatusList != null && tableStatusList.size() > 0) {

                for (RestaurantTableMO restaurantTableMO : tableStatusList) {

                    JSONObject jsonObject = new JSONObject();

                    jsonObject.put("id", restaurantTableMO.getId());

                    jsonObject.put("status", restaurantTableMO.getStatus());

                    jsonObject.put("name", restaurantTableMO.getName());

                    jsonObject.put("englishName", restaurantTableMO.getEnglishName());

                    jsonObject.put("chineseName", restaurantTableMO.getChineseName());

                    jsonObject.put("num", restaurantTableMO.getNum());

                    jsonObject.put("seatAmount", restaurantTableMO.getSeatAmount());

                    jsonObject.put("qrCodeSign", restaurantTableMO.getQrCodeSign());

                    jsonObject.put("qrCodeUrl", tableQrCodeService.getQrHost()+adminPath + tableQrCodeService.createQrCodeUrl(restaurantTableMO.getQrCodeSign()));

                    jsonArray.add(jsonObject);

                }

            }

        }

        result.setProperty("tableList", jsonArray);

        return result;

    }

    @RequestMapping("/delete")
    public Result delete(String data) {

        Result result = new Result(ResultCode.COMMON_SUCCESS);

        JSONObject obj = JSONObject.parseObject(data);

        String id = obj.getString("id");

        RestaurantTable restaurantTable = new RestaurantTable();

        restaurantTable.setId(id);

        restaurantTableService.delete(restaurantTable);

        return result;

    }

    @RequestMapping("/save")
    public Result save(String data) {

        Result result = new Result(ResultCode.COMMON_SUCCESS);

        RestaurantTableDTO restaurantTableDTO = JSONObject.parseObject(data, RestaurantTableDTO.class);

        restaurantTableService.saveOrUpdate(restaurantTableDTO);

        return result;

    }

    /**
     * 获取未使用的桌号
     * 关联订单表和桌号表，获取桌号相关信息
     * 对象中只需三个参数:餐厅ID，订单ID，更换的餐桌ID
     * @return
     */
    @RequestMapping(value = "/getFreeTab",method  = RequestMethod.POST)
    public Result  getFreeRestaurantTable()
    {
        Result restultResult = new Result(ResultCode.COMMON_SUCCESS);
        Restaurant restaurant = null;
        List<RestaurantTable> freeTableList = null;
        try
        {
            restaurant = ContextHolder.currentRestaurant();
            freeTableList = restaurantTableService.getFreeRestaurantTable(restaurant);
            if( ! (null == freeTableList || 0 == freeTableList.size()))
            {
            	restultResult.setProperty("freeTableList", freeTableList);
            }
            
        }catch(Exception e)
        {
        	throw new BusinessException(I18nUtil.getMessage("table.freelist.error.exception"));
        }
        return restultResult;
    }
}
